package com.luck.config;


import cn.hutool.core.util.ObjectUtil;
import feign.Logger;
import feign.RequestInterceptor;
import org.slf4j.MDC;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Configuration
public class FeignConfig {
    private static final String MDCTRACE_ID = "traceId";
    @Bean //日志级别
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
    @Bean
    public RequestInterceptor requestInterceptor() {
        return requestTemplate -> {
            requestTemplate.header(MDCTRACE_ID, MDC.get(MDCTRACE_ID));
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) (RequestContextHolder.getRequestAttributes());
            String token = requestAttributes.getRequest().getHeader("token");
            if (ObjectUtil.isNotEmpty(token)){
                requestTemplate.header("token",token);
            }
        };
    }
}
